package gaml.text;

import gaml.error.GamlException;
import gaml.yaml.YamlNode;

import java.util.LinkedHashMap;


/**
 * collection of yaml node printers
 * @author shnireck
 *
 */
public class YamlPrinterStore {
	LinkedHashMap <Class <? extends YamlNode>, YamlNodePrinter <? extends YamlNode>> printers = new LinkedHashMap <Class <? extends YamlNode>, YamlNodePrinter <? extends YamlNode>> ();
	YamlNodePrinter <YamlNode> defaultPrinter = null;
	
	/**
	 * new yaml printers store requires default printer
	 * @param defaultPrinter
	 */
	public YamlPrinterStore (YamlNodePrinter <YamlNode> defaultPrinter) { this.defaultPrinter = defaultPrinter; register (defaultPrinter); }
	
	/**
	 * adds new yaml printer to collection
	 * @param <T> type of yaml node to print
	 * @param newPrinter printer to add to collection
	 * @return host object
	 */
	public <T extends YamlNode> YamlPrinterStore register (YamlNodePrinter <T> newPrinter) { printers.put (newPrinter.getPrintNodeClass (), newPrinter); return this; }
	
	/**
	 * check if we have a special printer
	 * for yaml node
	 * @param <T> type of node to check
	 * @param node yaml node to ceck
	 * @return true if we have a special printer for yaml node, false if we don't
	 */
	public <T extends YamlNode> boolean hasPrinter (T node) { return printers.containsKey (node.getClass ()); }
	
	public <T extends YamlNode> YamlNodePrinter <T> getPrinter (T node) throws GamlException {
		if (hasPrinter (node)) return (YamlNodePrinter <T>) printers.get (node.getClass ());
		throw new GamlException (String.format ("No printer for node %s", node.toString ()));
	}
	
	/**
	 * get yaml node printer, set as default for store
	 * @return default yaml node printer
	 */
	public YamlNodePrinter <YamlNode> getDefaultPrinter () { return defaultPrinter; }
}
